package channel

type (
	ConnectContext interface {
		Context
	}
	DisconnectContext interface {
		Context
	}
	ActiveContext interface {
		Context
	}
	InactiveContext interface {
		Context
	}
	ConnectHandler interface {
		HandleConnect(ctx ConnectContext) error
	}
	DisconnectHandler interface {
		HandleDisconnect(ctx DisconnectContext) error
	}
	ActiveHandler interface {
		HandleActive(ctx ActiveContext) error
	}
	InactiveHandler interface {
		HandleInactive(ctx InactiveContext) error
	}
)

type connectContext struct {
	commonContext
}

func (c *connectContext) Next() error {
	c.index++
	for c.index < int8(len(c.handlers)) {
		if h, ok := c.handlers[c.index].(ConnectHandler); ok {
			err := h.HandleConnect(c)
			if err != nil {
				return err
			}
		}
		c.index++
	}
	return nil
}

type disconnectContext struct {
	commonContext
}

func (d *disconnectContext) Next() error {
	d.index++
	for d.index < int8(len(d.handlers)) {
		if h, ok := d.handlers[d.index].(DisconnectHandler); ok {
			err := h.HandleDisconnect(d)
			if err != nil {
				return err
			}
		}
		d.index++
	}
	return nil
}

type activeContext struct {
	commonContext
}

func (a *activeContext) Next() error {
	a.index++
	for a.index < int8(len(a.handlers)) {
		if h, ok := a.handlers[a.index].(ActiveHandler); ok {
			err := h.HandleActive(a)
			if err != nil {
				return err
			}
		}
		a.index++
	}
	return nil
}

type inactiveContext struct {
	commonContext
}

func (i *inactiveContext) Next() error {
	i.index++
	for i.index < int8(len(i.handlers)) {
		if h, ok := i.handlers[i.index].(InactiveHandler); ok {
			err := h.HandleInactive(i)
			if err != nil {
				return err
			}
		}
		i.index++
	}
	return nil
}
